package com.pragmatic.controller;

import com.alibaba.fastjson.JSON;
import com.pragmatic.cryptography.CallerSDKClient;
import com.pragmatic.cryptography.EncryptMessage;
import com.pragmatic.domain.UserInfo;
import com.pragmatic.util.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.client.HttpClient;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * ...
 *
 * @author wangdong
 * @since 2022/9/28 11:19
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

  @Resource private CallerSDKClient callerSDKClient;

  /**
   * 查询用户信息（明文传输）
   *
   * @param message 用户对象信息
   */
  @RequestMapping(value = "/findUser/plaintext", method = RequestMethod.POST)
  public String getUserInfoPlaintext(@RequestBody String message) {
    // 明文调用
    log.info("caller==明文-请求参数={}", message);
    String response =
        HttpClientUtil.sendPostJsonStr(
            "http://localhost:8081/user/getUserInfo/plaintext", message, 100000);
    log.info("caller==明文-响应结果={}", response);
    return response;
  }

  /**
   * 查询用户信息(密文字符串)
   *
   * @param message 用户对象信息
   */
  @RequestMapping(value = "/findUser/cryptography", method = RequestMethod.POST)
  public void getUserInfoCryptography(@RequestBody String message) {
    log.info("caller==请求参数-明文====={}", JSON.toJSONString(message));
    // 加密、加签
    EncryptMessage encryptMessage = callerSDKClient.doEncrypt(message);
    log.info("caller==请求参数-密文====={}", JSON.toJSONString(encryptMessage));
    String responseString =
        HttpClientUtil.sendPostJsonStr(
            "http://localhost:8081/user/getUserInfo/cryptography",
            JSON.toJSONString(encryptMessage),
            100000);
    log.info("caller==响应结果-密文====={}", JSON.toJSONString(encryptMessage));
    // 验签、解密
    String decrypt = callerSDKClient.doDecrypt(responseString);
    log.info("caller==响应结果-明文====={}", decrypt);
  }
}
